package cn.bdqn.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class UserCountListener implements HttpSessionListener {

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		//访问量+1
		ServletContext context = se.getSession().getServletContext();
		Integer count = (Integer) context.getAttribute("count");
		if(count==null){
			count = 1;
		}else{
			count++;
		}
		//把count放入到共享作用域中
		context.setAttribute("count", count);

	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		//访问量-1
		ServletContext context = se.getSession().getServletContext();
		Integer count = (Integer) context.getAttribute("count");
		if(count==null){
			count = 0;
		}else{
			count--;
		}
		//把count放入到共享作用域中
		context.setAttribute("count", count);

	}

}
